import 'package:flutter/scheduler.dart';
import 'package:get_storage/get_storage.dart';
import 'package:hockey/jzx_apps.dart';
import 'package:hockey/jzx_models.dart';
import 'package:hockey/pages/jzx_home/widgets/jzx_condition_pop.dart';
import 'package:hockey/services/jzx_app.dart';
import 'package:hockey/services/jzx_config.dart';
import 'package:hockey/services/jzx_user.dart';

import 'widgets/jzx_first_pop.dart';

class JzxHomeController extends GetxController {
  /// 当前等级
  // var user = JzxUserService.to.user;

  /// 当前等级
  var userLevel = JzxUserService.to.userLevel;

  /// 当前穿戴状态
  var userWear = JzxUserService.to.userWear;

  @override
  void onInit() {
    super.onInit();

    JzxConfigService.to.updateUsersAndTidyList();

    // JzxLogPvManger.eventTracking(page: JzxLogPages.home_tab);

    SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
      GetStorage box = GetStorage();
      // box.remove(kFirstHomePop);
      final state = box.read(kNotShowLevelPop);
      if (state == null) {
        box.write(kNotShowLevelPop, true);
        Get.dialog(
          const FirstLaunchPopView(),
          barrierColor: myAppColors.barrierColor,
          barrierDismissible: false,
        ).then((value) {
          JzxUserService.to.updateUserLevel(1);
          JzxUserService.to.updateUserWear(1);
          // level.value = 1;
          // wearCur.value = 1;
        });
      } else {
        // JzxUserService.to.updateUserLevel(1);
        // JzxUserService.to.updateUserWear();
      }
    });

    // 任务完成提示
    JzxAppService.bus.on<FulfilTask>().listen((event) {
      final item = JzxUserService.to.frameList[event.level - 1];
      Get.dialog(
        JzxConditionPopView(item),
        barrierColor: myAppColors.barrierColor,
        // barrierDismissible: false,
      );
    });
  }

  void applyWear(int level) {
    if (userWear.value == level) {
      return;
    }
    JzxUserService.to.updateUserWear(level);
    showMessage('Successfully');

    JzxAppService.bus.fire(RefreshPostLike());
  }

  /// 显示解锁条件
  showUnlockConditions(Rx<JzxCdtItem> item) {
    Get.dialog(
      JzxConditionPopView(item),
      barrierColor: myAppColors.barrierColor,
      // barrierDismissible: false,
    );
  }
}
